package pers.cj.pdos.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import pers.cj.pdos.entity.ArrearsOrder;
import pers.cj.pdos.entity.ArrearsWait;
import pers.cj.pdos.entity.PayOrder;
import pers.cj.pdos.entity.PayWait;
import pers.cj.pdos.exception.CustomException;
import pers.cj.pdos.mapper.ArrearsOrderMapper;
import pers.cj.pdos.mapper.ArrearsWaitMapper;
import pers.cj.pdos.mapper.SettAccountMapper;
import pers.cj.pdos.service.IArrearsOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import pers.cj.pdos.utils.UuidUtils;
import pers.cj.pdos.vo.ArrearsOrderVo;
import pers.cj.pdos.vo.PayWaitVo;

import java.math.BigDecimal;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chenj
 * @since 2019-11-04
 */
@Service
public class ArrearsOrderServiceImpl extends ServiceImpl<ArrearsOrderMapper, ArrearsOrder> implements IArrearsOrderService {
    @Autowired
    ArrearsOrderMapper arrearsOrderMapper;
    @Autowired
    ArrearsWaitMapper arrearsWaitMapper;
    @Autowired
    SettAccountMapper settAccountMapper;

    @Override
    public boolean checkNo(String no, String companyId) {
        return arrearsOrderMapper.selectCount(new QueryWrapper<ArrearsOrder>().eq("company_id",companyId).eq("no",no)) > 0;
    }


    @Override
    public boolean checkClose(ArrearsOrder arrearsOrder) {
        return arrearsOrderMapper.selectCount(new QueryWrapper<ArrearsOrder>().eq("id",arrearsOrder.getId()).eq("close",true)) > 0;
    }


    @Override
    public void insert(ArrearsOrder arrearsOrder) {
        arrearsOrderMapper.insert(arrearsOrder);

        //收款 余额+
        settAccountMapper.addBalance(arrearsOrder.getThisAmt(), arrearsOrder.getSettAccountId());

        ArrearsWait dbArrearsWait = arrearsWaitMapper.selectOne(new QueryWrapper<ArrearsWait>().eq("custom_id",arrearsOrder.getCustomId()).eq("company_id", arrearsOrder.getCompanyId()));

        if (dbArrearsWait != null) {
            ArrearsWait arrearsWait = new ArrearsWait();
            arrearsWait.setId(dbArrearsWait.getId());
            arrearsWait.setWaitIn(dbArrearsWait.getWaitIn().subtract(arrearsOrder.getTotalAmt()));
            arrearsWait.setHasIn(dbArrearsWait.getHasIn().add(arrearsOrder.getThisAmt()));
            arrearsWait.setDiscount(dbArrearsWait.getDiscount().add(arrearsOrder.getDiscount()));
            arrearsWaitMapper.updateById(arrearsWait);
        } else {
            ArrearsWait arrearsWait = new ArrearsWait();
            arrearsWait.setId(UuidUtils.getUUID());
            arrearsWait.setCustomId(arrearsOrder.getCustomId());
            arrearsWait.setTotal(new BigDecimal("0"));
            arrearsWait.setHasIn(arrearsOrder.getThisAmt());
            arrearsWait.setDiscount(arrearsOrder.getDiscount());
            arrearsWait.setWaitIn(arrearsOrder.getTotalAmt().negate());
            arrearsWait.setCompanyId(arrearsOrder.getCompanyId());
            arrearsWaitMapper.insert(arrearsWait);
        }
    }

    @Override
    public void close(ArrearsOrder arrearsOrder) {
        ArrearsOrder db = arrearsOrderMapper.selectById(arrearsOrder.getId());
        if (db == null) {
            throw new CustomException("参数id不正确，未找到原收款单");
        }


        //收款作废 余额-
        settAccountMapper.addBalance(db.getThisAmt().negate(), db.getSettAccountId());

        ArrearsWait dbArrearsWait = arrearsWaitMapper.selectOne(new QueryWrapper<ArrearsWait>().eq("custom_id",db.getCustomId()).eq("company_id", db.getCompanyId()));
        if (dbArrearsWait != null) {
            ArrearsWait wait = new ArrearsWait();
            wait.setId(dbArrearsWait.getId());
            wait.setWaitIn(dbArrearsWait.getWaitIn().add(db.getTotalAmt()));
            wait.setHasIn(dbArrearsWait.getHasIn().subtract(db.getThisAmt()));
            wait.setDiscount(dbArrearsWait.getDiscount().subtract(db.getDiscount()));
            arrearsWaitMapper.updateById(wait);
        }

        ArrearsOrder temp = new ArrearsOrder();
        temp.setId(db.getId());
        temp.setClose(true);
        arrearsOrderMapper.updateById(temp);
    }

    @Override
    public ArrearsOrderVo getVoById(String id) {
        return arrearsOrderMapper.getVoById(id);
    }
}
